package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wgq
 * @date 2019-10-21 16:34
 * 角色管理模块
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    /**
     * 登录用户查询  --> 角色查询
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.findPage(queryPageBean);
        return pageResult;
    }
    /**
     * 角色管理：新增角色数据
     * @param role
     * @param permissionIds
     */
    //@PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds){
        try {
            roleService.addRole(role, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL,e.getMessage());
        }
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    //@PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL,e.getMessage());
        }
        return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
    }
    /**
     * 编辑窗口弹出，根据行 ID 回显数据
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Role role;
        try {
            role = roleService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);

    }
    /**
     * 编辑角色
     * @param role
     * @param permissionIds
     * @return
     */
    //@PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds){
        try {
            roleService.edit(role, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL, e.getMessage());
        }
        return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
    }
    /**
     * 查询角色和权限之间的  对应关系
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id){
        try {
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 查询所有角色，用于用户新建窗口显示
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Role> roleList = roleService.findAllRole();
        if(roleList != null && roleList.size() > 0){
            return new Result(true,MessageConstant.QUERY_PERMISSION_SUCCESS,roleList);
        }
        return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
    }




}
